# -- coding: utf-8 --
"""测试存入mongo的二进制pdf 转存到文件中，打开后可以读取"""
import os

from spidertools.db_utils import mongdb_utils

project_env = os.environ.get("PROJECT_ENV")
# myclient, mydb = mongdb_utils.get_mongo_connect("mongodb://192.168.2.181:27017")
myclient, mydb = mongdb_utils.get_mongo_connect("mongodb://localhost:27017")
# if project_env != None and project_env == 'test_pdf':
#     mycol = mydb['project_html_test']
# else:
mycol = mydb['project_html']

search_result = mycol.find({
    # 'origin_url': "http://www.zjpubservice.com/jyxxgk/002001/002001001/20201117/d1c54038-db44-42ee-98f3-069916ad5002.html"})
    # 'origin_url': "http://zbtb.gd.gov.cn/bid/detailZgysgg?id=620ff73cdf064391bdb30d8622296c02"
    # 'origin_url': "http://zbtb.gd.gov.cn/bid/detailZgysgg?id=ac88e873e03745f9a64b386ec0c6d263"
    # 'origin_url': "http://zbtb.gd.gov.cn/bid/detailZbjg?id=03168d74aaa94d3eb941f5b041f6c9b9"
    # 'origin_url': "http://bidding.hunan.gov.cn/bidding/notice/40443?isdetail=1",
    "source_type": "湖南省招标投标监管网"
})

for index, item in enumerate(search_result):
    if "file_type" in item:
        print(item["file_type"])
        with open(f'xiaowu{index}_demo_use.{item["file_type"]}', "wb+") as f:
        # with open(f'111{index}_demo_use.html', "wb+") as f:
            f.write(item["file_content"])